{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ljung-Box test and Box-Pierce Test\n",
    "\n",
    "a statistical test that checks if autocorrelation exists in a time series."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open*</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close**</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Market Cap</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-08-03</td>\n",
       "      <td>22981.30</td>\n",
       "      <td>23578.65</td>\n",
       "      <td>22747.84</td>\n",
       "      <td>22846.51</td>\n",
       "      <td>26288169966</td>\n",
       "      <td>436633304289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-08-02</td>\n",
       "      <td>23308.43</td>\n",
       "      <td>23415.04</td>\n",
       "      <td>22710.08</td>\n",
       "      <td>22978.12</td>\n",
       "      <td>28389250717</td>\n",
       "      <td>439128030642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-08-01</td>\n",
       "      <td>23336.72</td>\n",
       "      <td>23464.79</td>\n",
       "      <td>22890.80</td>\n",
       "      <td>23314.20</td>\n",
       "      <td>25849159141</td>\n",
       "      <td>445527356896</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-07-31</td>\n",
       "      <td>23652.07</td>\n",
       "      <td>24121.64</td>\n",
       "      <td>23275.70</td>\n",
       "      <td>23336.90</td>\n",
       "      <td>23553591896</td>\n",
       "      <td>445941074223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-07-30</td>\n",
       "      <td>23796.82</td>\n",
       "      <td>24572.58</td>\n",
       "      <td>23580.51</td>\n",
       "      <td>23656.21</td>\n",
       "      <td>28148218301</td>\n",
       "      <td>452020722099</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date     Open*      High       Low   Close**       Volume  \\\n",
       "0 2022-08-03  22981.30  23578.65  22747.84  22846.51  26288169966   \n",
       "1 2022-08-02  23308.43  23415.04  22710.08  22978.12  28389250717   \n",
       "2 2022-08-01  23336.72  23464.79  22890.80  23314.20  25849159141   \n",
       "3 2022-07-31  23652.07  24121.64  23275.70  23336.90  23553591896   \n",
       "4 2022-07-30  23796.82  24572.58  23580.51  23656.21  28148218301   \n",
       "\n",
       "     Market Cap  \n",
       "0  436633304289  \n",
       "1  439128030642  \n",
       "2  445527356896  \n",
       "3  445941074223  \n",
       "4  452020722099  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import statsmodels.api as sm\n",
    "import pandas as pd\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "# df = pd.read_excel('./Crypto Pricing Data.xlsx', sheet_name='BTC', header=1) # header : which row to be column header, 1 means 2nd row in sheet.\n",
    "df = pd.read_excel('./Crypto Pricing Data.xlsx', sheet_name='BTC', skiprows=1).drop('x', axis=1)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>lb_stat</th>\n",
       "      <th>lb_pvalue</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000140</td>\n",
       "      <td>9.905646e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.028693</td>\n",
       "      <td>9.857561e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.962803</td>\n",
       "      <td>8.102515e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.711420</td>\n",
       "      <td>4.464671e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.752206</td>\n",
       "      <td>5.856144e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129</th>\n",
       "      <td>294.548678</td>\n",
       "      <td>5.720238e-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>305.735918</td>\n",
       "      <td>3.430189e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131</th>\n",
       "      <td>307.265510</td>\n",
       "      <td>3.391339e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>314.500455</td>\n",
       "      <td>6.275237e-17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>321.674945</td>\n",
       "      <td>1.165108e-17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>133 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        lb_stat     lb_pvalue\n",
       "1      0.000140  9.905646e-01\n",
       "2      0.028693  9.857561e-01\n",
       "3      0.962803  8.102515e-01\n",
       "4      3.711420  4.464671e-01\n",
       "5      3.752206  5.856144e-01\n",
       "..          ...           ...\n",
       "129  294.548678  5.720238e-15\n",
       "130  305.735918  3.430189e-16\n",
       "131  307.265510  3.391339e-16\n",
       "132  314.500455  6.275237e-17\n",
       "133  321.674945  1.165108e-17\n",
       "\n",
       "[133 rows x 2 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#fit ARMA model to dataset\n",
    "res = sm.tsa.ARMA(df[\"Close**\"], (1,1)).fit(disp=-1)\n",
    "\n",
    "#perform Ljung-Box test on residuals with lag=5 随机白噪声检验, if the 2nd returned value > 0.05, the TS is white noice series\n",
    "sm.stats.acorr_ljungbox(res.resid, return_df=True, auto_lag=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Box-Pierce Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>lb_stat</th>\n",
       "      <th>lb_pvalue</th>\n",
       "      <th>bp_stat</th>\n",
       "      <th>bp_pvalue</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.000140</td>\n",
       "      <td>9.905646e-01</td>\n",
       "      <td>0.000140</td>\n",
       "      <td>9.905754e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.028693</td>\n",
       "      <td>9.857561e-01</td>\n",
       "      <td>0.028605</td>\n",
       "      <td>9.857992e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.962803</td>\n",
       "      <td>8.102515e-01</td>\n",
       "      <td>0.959156</td>\n",
       "      <td>8.111336e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3.711420</td>\n",
       "      <td>4.464671e-01</td>\n",
       "      <td>3.695203</td>\n",
       "      <td>4.488240e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.752206</td>\n",
       "      <td>5.856144e-01</td>\n",
       "      <td>3.735772</td>\n",
       "      <td>5.880497e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>129</th>\n",
       "      <td>294.548678</td>\n",
       "      <td>5.720238e-15</td>\n",
       "      <td>279.364660</td>\n",
       "      <td>4.097400e-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>130</th>\n",
       "      <td>305.735918</td>\n",
       "      <td>3.430189e-16</td>\n",
       "      <td>289.426355</td>\n",
       "      <td>3.719264e-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>131</th>\n",
       "      <td>307.265510</td>\n",
       "      <td>3.391339e-16</td>\n",
       "      <td>290.800888</td>\n",
       "      <td>3.805138e-14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>132</th>\n",
       "      <td>314.500455</td>\n",
       "      <td>6.275237e-17</td>\n",
       "      <td>297.296899</td>\n",
       "      <td>9.177774e-15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>133</th>\n",
       "      <td>321.674945</td>\n",
       "      <td>1.165108e-17</td>\n",
       "      <td>303.733160</td>\n",
       "      <td>2.224028e-15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>133 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        lb_stat     lb_pvalue     bp_stat     bp_pvalue\n",
       "1      0.000140  9.905646e-01    0.000140  9.905754e-01\n",
       "2      0.028693  9.857561e-01    0.028605  9.857992e-01\n",
       "3      0.962803  8.102515e-01    0.959156  8.111336e-01\n",
       "4      3.711420  4.464671e-01    3.695203  4.488240e-01\n",
       "5      3.752206  5.856144e-01    3.735772  5.880497e-01\n",
       "..          ...           ...         ...           ...\n",
       "129  294.548678  5.720238e-15  279.364660  4.097400e-13\n",
       "130  305.735918  3.430189e-16  289.426355  3.719264e-14\n",
       "131  307.265510  3.391339e-16  290.800888  3.805138e-14\n",
       "132  314.500455  6.275237e-17  297.296899  9.177774e-15\n",
       "133  321.674945  1.165108e-17  303.733160  2.224028e-15\n",
       "\n",
       "[133 rows x 4 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sm.stats.acorr_ljungbox(res.resid, return_df=True, auto_lag=True, boxpierce=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tbquant",
   "language": "python",
   "name": "tbquant"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
